package com.bawei.caohao20240805.base

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import java.lang.reflect.ParameterizedType

/**
 * 作者: 曹浩
 * 时间: 2024/8/5
 * 功能描述:
 */
abstract class BaseActivity<V:ViewModel,D:ViewDataBinding>(private var layout:Int) :AppCompatActivity(){
    lateinit var mViewModel:V
    lateinit var mDing:D
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mDing=DataBindingUtil.setContentView(this,layout)
        mDing.lifecycleOwner=this
        var type = javaClass.genericSuperclass as ParameterizedType
        var vClass = type.actualTypeArguments[0] as Class<V>
        mViewModel=ViewModelProvider(this)[vClass]
        initView()
        dataObSer()
    }

    abstract fun dataObSer()

    abstract fun initView()

    override fun onDestroy() {
//        18．避免内存泄漏
        super.onDestroy()
        if(mDing!=null){
            mDing.unbind()
        }
    }

}